package com.lrd.snapshotorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrd.snapshotorder.entity.SenderAddress;
import com.lrd.snapshotorder.mapper.SenderAddressMapper;
import com.lrd.snapshotorder.protocol.ResultData;
import com.lrd.snapshotorder.protocol.dto.address.SenderAddressListDto;
import com.lrd.snapshotorder.protocol.dto.address.SenderAddressQueryDto;
import com.lrd.snapshotorder.service.SenderAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SenderAddressServiceImpl implements SenderAddressService {

    @Autowired
    private SenderAddressMapper senderAddressMapper;

    @Override
    public ResultData<List<SenderAddressListDto>> querySenderAddressList(SenderAddressQueryDto dto) {
        Page<SenderAddress> senderAddressPage = senderAddressMapper.querySenderAddressList(dto);
        if (CollUtil.isNotEmpty(senderAddressPage.getRecords())) {
            List<SenderAddressListDto> senderAddressListDtos = BeanUtil.copyToList(senderAddressPage.getRecords(), SenderAddressListDto.class);
            return new ResultData<>(0, "", senderAddressListDtos, senderAddressPage.getTotal());
        }
        return new ResultData<>(0, "发货人地址列表为空", new ArrayList<>(), 0L);
    }
}
